package top.yokey.android.http;

import android.app.Application;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;

public class HttpHelp {
    private static volatile HttpHelp httpHelp;
    private String apiUrl = "";
    private String queryUrl = "";
    private HashMap<String, String> common = null;

    public static HttpHelp get() {
        if (httpHelp == null) {
            synchronized (HttpHelp.class) {
                if (httpHelp == null) {
                    httpHelp = new HttpHelp();
                }
            }
        }
        return httpHelp;
    }

    public void init(Application app, String apiUrl, HashMap<String, String> hashMap) {
        this.apiUrl = apiUrl;
        this.common = hashMap;
        OkGo.getInstance().init(app);
    }

    public HttpHelp ready(String controller, String action) {
        this.queryUrl = this.apiUrl + controller + "/" + action + "?";
        return httpHelp;
    }

    public HttpHelp add(String key, String value) {
        this.queryUrl += key + "=" + value + "&";
        return httpHelp;
    }

    public void get(BaseCallBack baseCallBack) {
        for (String key : common.keySet()) {
            String value = common.get(key);
            queryUrl += key + "=" + value + "&";
        }
        System.out.println(queryUrl);
        OkGo.<String>get(queryUrl).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                try {
                    JSONObject jsonObject = new JSONObject(body);//将JSON字符串赋值给JSON对象
                    String code = jsonObject.getString("code");//获取code字段的值
                    String msg = jsonObject.getString("msg");//获取msg字段的值
                    if (code.equals("2")) {
                        if (baseCallBack != null) {
                            baseCallBack.onError(msg);
                            return;
                        }
                    }
                    if (baseCallBack != null) {
                        baseCallBack.onSuccess(jsonObject.getString("data").replace("null","\"\""));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (baseCallBack != null) {
                    baseCallBack.onError(response.message());
                }
            }
        });
    }

    public void post(File file, BaseCallBack baseCallBack) {
        for (String key : common.keySet()) {
            String value = common.get(key);
            queryUrl += key + "=" + value + "&";
        }
        System.out.println(queryUrl);
        OkGo.<String>post(queryUrl).params("image", file).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                try {
                    JSONObject jsonObject = new JSONObject(body);//将JSON字符串赋值给JSON对象
                    String code = jsonObject.getString("code");//获取code字段的值
                    String msg = jsonObject.getString("msg");//获取msg字段的值
                    if (code.equals("2")) {
                        if (baseCallBack != null) {
                            baseCallBack.onError(msg);
                            return;
                        }
                    }
                    if (baseCallBack != null) {
                        baseCallBack.onSuccess(jsonObject.getString("data").replace("null","\"\""));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (baseCallBack != null) {
                    baseCallBack.onError(response.message());
                }
            }
        });
    }

    public void setToken(String token) {

        this.common.put("token", token);

    }

}
